package com.banciyuan.bcywebview.biz.circles.adapter.list;

import android.content.Context;
import android.os.Build;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.view.textview.FocusButton;
import com.banciyuan.bcywebview.biz.circles.CircleLikeHelper;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleSmoothActivity;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.AcgItem;

/**
 * Created by squall on 2016/5/4.
 */
public class CircleProductionAdapter extends BaseAdapter {

    private List<AcgItem> mlist = new ArrayList<>();
    private Context mContext;
    private LayoutInflater inflater;
    private RequestQueue mQueue;

    public CircleProductionAdapter(List<AcgItem> mlist, Context mContext) {
        this.mlist = mlist;
        this.mContext = mContext;
        this.inflater = LayoutInflater.from(mContext);
        mQueue = VolleyQueue.getRquest(mContext);
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return mlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return position == 0 ? 0 : 1;
    }

    private class ViewHolder {
        public RoundedImageView img;
        public ImageView img_normal;
        public ImageView rank;
        public TextView name;
        public TextView count;
        public FocusButton like;
        public RelativeLayout work_line;

        public ViewHolder(View convertView) {
            rank = (ImageView) convertView.findViewById(R.id.circle_rank);
            img = (RoundedImageView) convertView.findViewById(R.id.iv_work);
            img_normal = (ImageView) convertView.findViewById(R.id.iv_work_normal);
            name = (TextView) convertView.findViewById(R.id.tv_workname);
            count = (TextView) convertView.findViewById(R.id.tv_postcount);
            like = (FocusButton) convertView.findViewById(R.id.tv_like);
            work_line = (RelativeLayout) convertView.findViewById(R.id.work_line);
        }
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        ViewHolder topHolder = null;
        if (convertView == null) {
            switch (getItemViewType(position)) {
                case 0:
                    convertView = inflater.inflate(R.layout.circle_production_item_top, null);
                    topHolder = new ViewHolder(convertView);
                    convertView.setTag(topHolder);
                    break;
                case 1:
                    convertView = inflater.inflate(R.layout.circle_production_item_normal, null);
                    viewHolder = new ViewHolder(convertView);
                    convertView.setTag(viewHolder);
                    break;
            }
        } else {
            switch (getItemViewType(position)) {
                case 0:
                    topHolder = (ViewHolder) convertView.getTag();
                    break;
                case 1:
                    viewHolder = (ViewHolder) convertView.getTag();
                    break;
            }
        }

        final AcgItem acgItem = mlist.get(position);
        switch (getItemViewType(position)) {
            case 0:
                setView(topHolder, acgItem, position);
                break;
            case 1:
                setView(viewHolder, acgItem, position);
                break;
        }

        return convertView;
    }

    private void setView(ViewHolder viewHolder, final AcgItem acgItem, final int position) {
        if (!TextUtils.isEmpty(acgItem.getCover())) {
            ImageLoader.getInstance().displayImage(acgItem.getCover(), viewHolder.img, ImageOptions.options);
            ImageLoader.getInstance().displayImage(acgItem.getCover(), viewHolder.img_normal, ImageOptions.options);
        }
        viewHolder.rank.setVisibility(View.VISIBLE);
        viewHolder.img_normal.setVisibility(View.VISIBLE);
        viewHolder.img.setVisibility(View.GONE);
        switch (position) {
            case 0:
                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
                    viewHolder.rank.setImageDrawable(mContext.getResources().getDrawable(R.drawable.top_one_back, mContext.getTheme()));
                } else {
                    viewHolder.rank.setImageDrawable(mContext.getResources().getDrawable(R.drawable.top_one_back));
                }
                break;
            case 1:
                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
                    viewHolder.rank.setImageDrawable(mContext.getResources().getDrawable(R.drawable.top_two_back, mContext.getTheme()));
                } else {
                    viewHolder.rank.setImageDrawable(mContext.getResources().getDrawable(R.drawable.top_two_back));
                }
                viewHolder.img.setBorderColor(mContext.getResources().getColorStateList(R.color.top_two_red));
                viewHolder.img.setVisibility(View.VISIBLE);
                viewHolder.img_normal.setVisibility(View.GONE);
                break;
            case 2:
                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
                    viewHolder.rank.setImageDrawable(mContext.getResources().getDrawable(R.drawable.top_three_back, mContext.getTheme()));
                } else {
                    viewHolder.rank.setImageDrawable(mContext.getResources().getDrawable(R.drawable.top_three_back));
                }
                viewHolder.img.setBorderColor(mContext.getResources().getColorStateList(R.color.top_three_yellow));
                viewHolder.img.setVisibility(View.VISIBLE);
                viewHolder.img_normal.setVisibility(View.GONE);
                break;
            default:
                viewHolder.rank.setVisibility(View.GONE);
                break;
        }
        if (!TextUtils.isEmpty(acgItem.getReal_name())) {
            viewHolder.name.setText(acgItem.getReal_name());
        }
        if (!TextUtils.isEmpty(acgItem.getPost_count())) {
            viewHolder.count.setText(Html.fromHtml(String.format(mContext.getString(R.string.week_production_unit), acgItem.getPost_count())));
        }
        if (acgItem.isWf_status()) {
            viewHolder.like.setSelected(true);
        } else {
            viewHolder.like.setSelected(false);
        }
        viewHolder.like.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!acgItem.isWf_status()) {
                    doLikeWork("follow", acgItem, v);
                }
            }
        });

        viewHolder.work_line.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoUtil.gotoAct(mContext, CircleSmoothActivity.class, acgItem.getReal_name(), acgItem.getId());
            }
        });
    }

    private void doLikeWork(String type, final AcgItem acgItem, final View view) {
        CircleLikeHelper.doCircleLike(mContext, acgItem.getId(), "work", type, new CircleLikeHelper.CircleLikeSuccess() {
            @Override
            public void onCircleLikeSuccess() {
                try {
                    acgItem.setWf_status(true);
                    view.setSelected(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
